<template>
	<view class="">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view v-for="(item, index) in couponList" :key="item.businessId">
				<coupon-cell 
				:businessId="item.businessId"
				:discountAmount="item.money"
				:rangInfo="item.conditionMoney"
				:activityType="item.couponName"
				:durationDate="item.useTime"
				:couponType="item.businessName"
				:storeRang = "item.applicableUnit"
				:isReceive="item.isReceive"
				:useNotice="item.useNotice"
				:useArea = "item.useArea"
				:businessType="0"
				:useTypeName="item.useTypeName"
				:applicableUnitType="item.applicableUnitType"
				@getCoupon="getCoupon"
				></coupon-cell>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import couponCell from '@/components/coupon/coupon-cell.vue'
	import {apiCouponList,receiveCoupons} from '@/api/marketing-api.js'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			couponCell
		},
		data() {
			return {
				couponList: []
			}
		},
		computed: {
		  isLogin() {
			  return this.$store.state.user.isLogin;
		  }	
		},
		onLoad() {
		},
		methods: {
			upCallback(page) {
				apiCouponList({
					pageNo : page.num,
					tenantId : this.$store.state.user.tenantId,
					businessNo : this.$store.state.order.businessNo,
					memberNo : this.$store.state.user.userInfo.memberNo
				}).then(res => {
					this.mescroll.endSuccess(res.data.length);
					//设置列表数据
					if (page.num == 1) this.couponList = []; //如果是第一页需手动制空列表
					this.couponList = this.couponList.concat(res.data); //追加新数据
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			getCoupon(data){
				if(this.isLogin) {
					receiveCoupons({
						businessId : data.businessId,
					}).then(res => {
						this.pageNo = 1;
						this.getData();
						uni.$emit('refreshCouponData');
					})
				}else {
					uni.navigateTo({
						url: '../../pages/login/login'
					})
				}
				
				
			},
			
		}
	}
</script>

<style lang="scss">

</style>
